package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.Setmeal;

import com.itheima.domain.SetmealDTO;
import com.itheima.domain.SetmealDish;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.SetmealDishMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.SetmealService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Arrays;
import java.util.List;

/**
 * @program: reggie_take_out
 * @className: SetmealServiceImpl
 * @description: Reggie员工端套餐管理业务类
 * @author: psh
 * @create: 2022-06-06 10:29
 **/
@Service
public class SetmealServiceImpl implements SetmealService {

	@Autowired
	private SetmealMapper setmealMapper;
	@Autowired
	private SetmealDishMapper setmealDishMapper;
	@Autowired
	private CategoryMapper categoryMapper;

	/**
	 *@Description:套餐列表展示
	 *@Param: Integer page, Integer pageSize, String name
	 *@return: Page<Setmeal>
	 *@Author: psh
	 *@Date: 2022/6/6 12:03
	 */
		@Override
		public Page<Setmeal> page(Integer page, Integer pageSize, String name) {
			//1.校验参数
			if(page < 1){
				page = 1;
			}
			if(pageSize<1){
				pageSize = 10;
			}
			//2.构建分页、查询条件
			Page<Setmeal> pageCondition = new Page<>(page,pageSize);
			LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
			if(StringUtils.isNotBlank(name)){
				wrapper.like(Setmeal::getName,name);
			}
			wrapper.orderByDesc(Setmeal::getUpdateTime);
			//3.执行查询
			Page<Setmeal> setmealPage = setmealMapper.selectPage(pageCondition, wrapper);
			return setmealPage;
		}
	/**
	 *@Description: 新增套餐
	 *@Param: SetmealDTO setmealDTO
	 *@return: int
	 *@Author: psh
	 *@Date: 2022/6/6 12:51
	 */
	@Override
	public int save(SetmealDTO setmealDTO) {
		int row = 0;
		//1.校验参数
		if (setmealDTO == null){
			return row;
		}

		//2.1 创建套餐setmeal对象
		Setmeal setmeal = new Setmeal();
		//2.2 将前端接收到的setmealDTO对象数据拷贝到setmeal对象中
		BeanUtils.copyProperties(setmealDTO,setmeal);
		//2.3 将套餐信息添加到数据库中进行校验
		row = setmealMapper.insert(setmeal);
		if(row < 1){
			return row;
		}
		//3.1 拿到菜品套餐集合
		List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
		//3.2 再新增套餐和菜品关联数据，把setmealDTO拿出来，存入setmeal_dish(菜品套餐关联表)表中
		if (setmealDishes != null){
			for (SetmealDish setmealDish : setmealDishes) {
				//将套餐的id值setmealId设置到菜品套餐关联表中
				setmealDish.setSetmealId(setmeal.getId());
				row = setmealDishMapper.insert(setmealDish);
			}
		}
		return row;
	}
	/**
	*@Description: 通过id获取SetmealDTO对象
	*@Param: Long id
	*@return: SetmealDTO
	*@Author: psh
	*@Date: 2022/6/11 0:23
	*/
	@Override
	public SetmealDTO findById(Long id) {
		SetmealDTO setmealDTO = null;
		//1.异常判断、校验
		if(id == null){
			return setmealDTO;
		}
		//2.根据套餐ID，获取套餐对象
		Setmeal setmeal = setmealMapper.selectById(id);
		if(setmeal == null){
			return setmealDTO;
		}
		//3.根据套餐ID，获取“套餐和菜品关联”的列表数据
		//3.1 构建“套餐和菜品关联”查询条件
		LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(SetmealDish::getSetmealId,id);
		//3.2 执行查询
		List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
		//4.封装数据，把“套餐”、“套餐和菜品关联”数据，封装到返回结果中
		setmealDTO = new SetmealDTO();
		//4.1 把“套餐”属性拷贝到setmealDTO对象中
		BeanUtils.copyProperties(setmeal,setmealDTO);
		//4.2 把“套餐和菜品关联”数据封装到setmealDTO对象中
		setmealDTO.setSetmealDishes(setmealDishes);
		return setmealDTO;
	}
	/**
	 *@Description: 修改套餐
	 *@Param: SetmealDTO setmealDTO
	 *@return: int
	 *@Author: psh
	 *@Date: 2022/6/6 14:02
	 */
	@Override
	public int update(SetmealDTO setmealDTO) {
		int row = 0;
		if (setmealDTO == null){
			return row;
		}
		//1.检测前端传递来的修改数据中的套餐名称是否已经存在
		//获取前端传递来的修改数据中的套餐名称
		String name = setmealDTO.getName();
		//根据名称重数据库查询数据
		LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Setmeal::getName,name);
		Setmeal setmealOne = setmealMapper.selectOne(wrapper);
		//如果数据库中存在该名称的套餐，并且id和前端传递的套餐的ID不一致，说明套餐名称已经存在，则不允许修改
		if (setmealOne != null && !setmealOne.getId().equals(setmealDTO.getId())){
			return row;
		}

		//2.拆分SetmealDTO数据 套餐表执行修改操作；套餐和菜品关系表执行先删除后新增操作；
		//修改setmeal表数据
		Setmeal setmeal = new Setmeal();
		BeanUtils.copyProperties(setmealDTO,setmeal);
		row = setmealMapper.updateById(setmeal);
		if (row < 1){
			return row;
		}
		//删除套餐菜品关联数据
		LambdaQueryWrapper<SetmealDish> setmealDishWrapper = new LambdaQueryWrapper<>();
		setmealDishWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
		setmealDishMapper.delete(setmealDishWrapper);
		//新增套餐菜品关联数据
		List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
		if (setmealDishes != null && setmealDishes.size() > 0){
			for (SetmealDish setmealDish : setmealDishes) {
				//防止出现主键冲突问题
				setmealDish.setId(null);
				//手动设置套餐id
				setmealDish.setSetmealId(setmeal.getId());
				setmealDishMapper.insert(setmealDish);

			}
		}
		return row;
	}
	/**
	 *@Description: 批量启售、停售
	 *@Param: Integer status, Long[] ids
	 *@return: void
	 *@Author: psh
	 *@Date: 2022/6/6 18:12
	 */
	@Override
	public void updateStatusByIds(Integer status, Long[] ids) {
		//1.构建更新条件 update setmeal set status = #{statsu} where id in #{ids}
		LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.set(Setmeal::getStatus,status);
		updateWrapper.in(Setmeal::getId,ids);
		//2.执行更新操作
		setmealMapper.update(new Setmeal(),updateWrapper);

	}
	/**
	 *@Description: 批量删除
	 *@Param: Long[] ids
	 *@return: int
	 *@Author: psh
	 *@Date: 2022/6/6 19:05
	 */
	@Override
	public int deleteByIds(Long[] ids) {
		//逻辑删除
		int row = setmealMapper.deleteBatchIds(Arrays.asList(ids));
		return row;
	}
	/**
	 *@Description: 根据分类、状态获取套餐列表
	 *@Param: Long categoryId, Integer status
	 *@return: List<Setmeal>
	 *@Author: psh
	 *@Date: 2022/6/6 21:39
	 */
	@Override
	public List<Setmeal> list(Long categoryId, Integer status) {
		List<Setmeal> setmealList = null;
		//1.校验、异常判断
		if(categoryId == null || status ==null){
			return setmealList;
		}
		//2.封装查询条件
		LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Setmeal::getCategoryId,categoryId);
		wrapper.eq(Setmeal::getStatus,status);
		//3.执行查询，返回结果
		setmealList = setmealMapper.selectList(wrapper);
		return setmealList;
	}
}
